import Vue from 'vue';
import Router from 'vue-router';
import store from '../store'

import router from './modules/constantRoutes' // 初始化路由
import log from './modules/log'
import error from './modules/error'
import menu from './modules/menu'
import role from './modules/role'
import user from './modules/user'
import center from './modules/center'
import setting from './modules/setting'
import home from './modules/home'
import notice from './modules/notice'
import business from './modules/business'
import Settled from './modules/Settled'
import enterCheck from "./modules/enterCheck"
import SetCategory from "./modules/SetCategory"
import brand from "./modules/brand"
// 进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use (Router);
let routerChild = [{
  path: '/admin',
  component: resolve => require(['../views/index'], resolve),
  children: [
    {
      path: '/setting',
      name: '账号配置',
      component: resolve => require(['../views/setting/BaseSetting'], resolve),
      meta: {
        title: '基础配置',
      }
    },
    {
      path: '/home',
      name: '首页',
      component: resolve => require(['../views/home'], resolve)
    },
    // {
    //   path: 'menu/configMenu',
    //   name: '系统管理/编辑菜单',
    //   component: resolve =>
    //     require (['../views/menu/configmenu'], resolve)
    // },
    // {
    //   path: 'menu/addMenu',
    //   name: '系统管理/新增菜单',
    //   component: resolve =>
    //     require (['../views/menu/addMenu'], resolve)
    // }
  ]
}]
let routers = new Router({
  // mode: 'history', 
  routes: [...routerChild, ...router] 
})
//解决编程式路由往同一地址跳转时会报错的情况
const originalPush = Router.prototype.push
const originalReplace = Router.prototype.replace
//push
Router.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}
//replace
Router.prototype.replace = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
    return originalReplace.call(this, location).catch(err => err)
}

// 标记是否已经拉取权限信息
let isFetchPermissionInfo = false
routers.beforeEach ((to, from, next) => {
  // 进度条
  NProgress.start ();
  // 拉取权限信息
  if (store.state.router.permissionRouter.length > 0) {
    if (!isFetchPermissionInfo) {
      let permissionRouter = store.state.router.permissionRouter
      store.dispatch('updatePermissionRouter', permissionRouter)
      // let hash = window.location.hash.split('#')[1]
      isFetchPermissionInfo = true
        routers.push({
          path: to.path
        })
    }
  }
  // console.log(isFetchPermissionInfo, 'isFetchPermissionInfo', store.state.router.permissionRouter)
  // 左侧title名字
  // console.log(to.path, 'to.path')
  // let arrRouter = [...brand,...SetCategory,...enterCheck,...business,...Settled,...notice,...home, ...log, ...center, ...setting, ...error, ...menu, ...role, ...user]
  // arrRouter.forEach((item) => {
  //   if (item.path === to.path) {
  //     let name = ''
  //     if (item.children) {
  //       name = item.children[0].name
  //     } else {
  //       name = item.name
  //     }
  //     let list = name.split('/')
  //     store.dispatch('setRouter', list)
  //   } else {
  //     let child = item.children
  //     if (child) {
  //       child.forEach((_item) => {
  //         let path = (to.path).split('/')
  //         let len = path.length - 1
  //         if (_item.path === path[len]) {
  //           // debugger
  //           let name = ''
  //           if (_item.children) {
  //             name = _item.children[0].name
  //           } else {
  //             name = _item.name
  //           }
  //           let list = name.split('/')
  //           store.dispatch('setRouter', list)
  //         }
  //       })
  //     }
  //   }
  // })
  next ();
});

routers.afterEach (() => {
  // 进度条
  NProgress.done ();
});

export default routers;
